package com.zj.guava;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.List;
import java.util.Objects;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;


public class PreconditionTest {

    @Test(expected = NullPointerException.class)
    public void checkNotNull(){
        checkNotNull(null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void checkArguments(){
        String name="aaaaaaaaaaaaa";
        Preconditions.checkArgument(name.length()>0&&name.length()<10);
        fail("can not to here");
    }

    @Test(expected=IndexOutOfBoundsException.class)
    public void checkIndex(){
        List<String> list= ImmutableList.of("a");
        Preconditions.checkElementIndex(1,list.size());
        fail("can not to here");

    }

    @Test(expected = NullPointerException.class)
    public void testObjects(){
        //java8的方法
        Objects.requireNonNull(null);
    }

    public <T> void checkNotNull(final List<T> list){
        Preconditions.checkNotNull(list);
    }
}
